/*
 * File : MA5801.c
 *
 */

#include "MS5801.h"
#include "stm32f10x.h"
#include "i2c.h"
#include "task.h"
#include "tool.h"
#include <stdlib.h>
#include <math.h>
#include "memory.h"
#include "xstring.h"
#include "console.h"
#include "LSM303DLH.h"
#include "ADXL345.h"
#include "L3G4200D.h"
#include "HMC5883.h"
#include <stdio.h>


#define MS5801_W		0xEC
#define MS5801_R        0xEC


unsigned int Coef[8] = {0};
unsigned long D1 = 0, D2 = 0;


#define I2C_FLAG	1
#define I2C_EVENT	2
unsigned int MS5801_Get_PROM(int idx)
{
    unsigned char buf[3];
    unsigned int tmp;
PROM:
	/* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto PROM;
	
    I2C_AcknowledgeConfig(I2C1, ENABLE);
		
	/* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto PROM;
		
	/* Send START condition */
	I2C_GenerateSTART(I2C1, ENABLE);
	
	/* Test on EV5 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_MODE_SELECT, I2C_EVENT) != 0)
		goto PROM;
	 
	/* Send slave addr for write */
	 I2C_Send7bitAddress(I2C1, MS5801_R, I2C_Direction_Transmitter);
	
	/* Test on EV6 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED, I2C_EVENT) != 0)
		goto PROM;
	
	/* Clear EV6 by setting again the PE bit */
	I2C_Cmd(I2C1, ENABLE);
	
	/* Send register addr to write to */
	I2C_SendData(I2C1, 0xA0+(idx*2));  
	
	/* Test on EV8 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED, I2C_EVENT) != 0)
		goto PROM;
    
    I2C_GenerateSTOP(I2C1, ENABLE);
    
	
    
    /* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto PROM;
    
	/* Send STRAT condition a second time */  
	I2C_GenerateSTART(I2C1, ENABLE);
	
	/* Test on EV5 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_MODE_SELECT, I2C_EVENT) != 0)
		goto PROM;
	
	/* Send slave addr for read */
	I2C_Send7bitAddress(I2C1, MS5801_R, I2C_Direction_Receiver);
	
	/* Test on EV6 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED, I2C_EVENT) != 0)
		goto PROM;
	
    
    buf[0] = I2C_ReceiveData(I2C1);
    if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED, I2C_EVENT) != 0)
			goto PROM;
    
    buf[1] = I2C_ReceiveData(I2C1);
    if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED, I2C_EVENT) != 0)
			goto PROM;
    
    buf[2] = I2C_ReceiveData(I2C1);
    if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED, I2C_EVENT) != 0)
			goto PROM;
    
    /* Disable Acknowledgement */
    I2C_AcknowledgeConfig(I2C1, DISABLE);
			
	/* Send STOP Condition */
	I2C_GenerateSTOP(I2C1, ENABLE);
    
    
    tmp = (int)(buf[1] << 8) + buf[2];
	
	return tmp;
}


void MS5801_Get_ADC_SET(int idx)
{
ADC_SET:
	/* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto ADC_SET;
	
    I2C_AcknowledgeConfig(I2C1, ENABLE);
		
	/* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto ADC_SET;
		
	/* Send START condition */
	I2C_GenerateSTART(I2C1, ENABLE);
	
	/* Test on EV5 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_MODE_SELECT, I2C_EVENT) != 0)
		goto ADC_SET;
	 
	/* Send slave addr for write */
	 I2C_Send7bitAddress(I2C1, MS5801_W, I2C_Direction_Transmitter);
	
	/* Test on EV6 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED, I2C_EVENT) != 0)
		goto ADC_SET;
	
	/* Clear EV6 by setting again the PE bit */
	I2C_Cmd(I2C1, ENABLE);
	
	/* Send register addr to write to */
    if(idx == 1)
	    I2C_SendData(I2C1, 0x48); 
    else if(idx == 2)
        I2C_SendData(I2C1, 0x58);
	
	/* Test on EV8 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED, I2C_EVENT) != 0)
		goto ADC_SET;
    
    I2C_GenerateSTOP(I2C1, ENABLE);
    
}

unsigned long MS5801_Get_ADC_CONV(void)
{
    unsigned char buf[4];
    unsigned long tmp;
	
ADC_CONV:
	
	/* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto ADC_CONV;
	
    I2C_AcknowledgeConfig(I2C1, ENABLE);
		
	/* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto ADC_CONV;
		
	/* Send START condition */
	I2C_GenerateSTART(I2C1, ENABLE);
	
	/* Test on EV5 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_MODE_SELECT, I2C_EVENT) != 0)
		goto ADC_CONV;
	 
	/* Send slave addr for write */
	 I2C_Send7bitAddress(I2C1, MS5801_W, I2C_Direction_Transmitter);
	
	/* Test on EV6 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED, I2C_EVENT) != 0)
		goto ADC_CONV;
	
	/* Clear EV6 by setting again the PE bit */
	I2C_Cmd(I2C1, ENABLE);
	
	/* Send register addr to write to */
	I2C_SendData(I2C1, 0x00);  
	
	/* Test on EV8 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED, I2C_EVENT) != 0)
		goto ADC_CONV;
    
    I2C_GenerateSTOP(I2C1, ENABLE);
    
	
    
    /* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto ADC_CONV;
    
	/* Send STRAT condition a second time */  
	I2C_GenerateSTART(I2C1, ENABLE);
	
	/* Test on EV5 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_MODE_SELECT, I2C_EVENT) != 0)
		goto ADC_CONV;
	
	/* Send slave addr for read */
	I2C_Send7bitAddress(I2C1, MS5801_R, I2C_Direction_Receiver);
	
	/* Test on EV6 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED, I2C_EVENT) != 0)
		goto ADC_CONV;
	
    
    buf[0] = I2C_ReceiveData(I2C1);
    if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED, I2C_EVENT) != 0)
			goto ADC_CONV;
    
    I2C_AcknowledgeConfig(I2C1, ENABLE);
    
    buf[1] = I2C_ReceiveData(I2C1);
    if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED, I2C_EVENT) != 0)
			goto ADC_CONV;
    
    I2C_AcknowledgeConfig(I2C1, ENABLE);
    
    buf[2] = I2C_ReceiveData(I2C1);
    if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED, I2C_EVENT) != 0)
			goto ADC_CONV;
    
    I2C_AcknowledgeConfig(I2C1, ENABLE);
    
    buf[3] = I2C_ReceiveData(I2C1);
    if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED, I2C_EVENT) != 0)
			goto ADC_CONV;
    
    /* Disable Acknowledgement */
    I2C_AcknowledgeConfig(I2C1, DISABLE);
			
	/* Send STOP Condition */
	I2C_GenerateSTOP(I2C1, ENABLE);
    
    
    tmp = (unsigned long)(buf[1] << 16) + (unsigned long)(buf[2] << 8) + buf[3];
	
	return tmp;
}

void MS5801_Reset(void)
{

Reset:
	/* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto Reset;
	
    I2C_AcknowledgeConfig(I2C1, ENABLE);
    
		
	/* While the bus is busy */
	if (I2C_Check(I2C1, I2C_FLAG_BUSY, I2C_FLAG) != 0)
		goto Reset;
		
	/* Send START condition */
	I2C_GenerateSTART(I2C1, ENABLE);
	
	/* Test on EV5 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_MODE_SELECT, I2C_EVENT) != 0)
		goto Reset;
	 
	/* Send slave addr for write */
	 I2C_Send7bitAddress(I2C1, MS5801_W, I2C_Direction_Transmitter);
	
	/* Test on EV6 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED, I2C_EVENT) != 0)
		goto Reset;
	
	/* Clear EV6 by setting again the PE bit */
	I2C_Cmd(I2C1, ENABLE);
	
	/* Send register addr to write to */
	I2C_SendData(I2C1, 0x1E); 
	
	/* Test on EV8 and clear it */
	if (I2C_Check(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED, I2C_EVENT) != 0)
		goto Reset;
    
    I2C_GenerateSTOP(I2C1, ENABLE);
    
}